var Feng = {
    ctxPath:"",
    addCtx: function (ctx) {
        if (this.ctxPath === "") {
            this.ctxPath = ctx;
        }
    }
};
Feng.addCtx(ctxPath);
Feng.info = function (info) {
    layer.msg(info, {icon: 6});
};
Feng.success = function (info) {
    layer.msg(info, {icon: 1});
};
Feng.error = function (info) {
    layer.msg(info, {icon: 2});
};
Feng.confirm = function (tip, ensure) {
    layer.confirm(tip, {
        skin: 'layui-layer-admin'
    }, function (index) {
        ensure(index);
    });
};

/**
 * 公共ajax提交
 * @param type
 * @param init
 * @param url
 * @param data
 * @param success
 * @param error
 */
 Feng.ajaxCommit = function( type, init, url, data, success, error, contentType) {
    $.ajax({
        async: true,
        type: type,
        url:  Feng.ctxPath + url,
        data: contentType == true ? data : JSON.stringify(data),
        contentType: contentType == true ? 'application/x-www-form-urlencoded;charset=utf-8' : 'application/json;charset=utf-8',
        beforeSend: function () {
           /* if(!init)
                layer.load(0, {shade: [0.4,'#fff']});*/
        },
        success: function (result) {
            success(result);
            layer.closeAll('loading');
        },
        error: function (result) {
            if(error){
                error(result);
            }else{
                if(result.responseJSON!=null && result.responseJSON!=undefined){
                    Feng.error(result.responseJSON.message + "!");
                }else{
                    Feng.error("请求失败!");
                }
            }
                layer.closeAll('loading');
        }
    });
};

//判断数据是否为Null或者undefined或者为空字符串
 Feng.checkIsNullOrEmpty = function(value) {
    //正则表达式用于判斷字符串是否全部由空格或换行符组成
    var reg = /^\s*$/;
    //返回值为true表示不是空字符串
    return (value != null && value != undefined && !reg.test(value));
}



/**
 * param 将要转为URL参数字符串的对象
 * key URL参数字符串的前缀
 * encode true/false 是否进行URL编码,默认为true
 * idx ,循环第几次，用&拼接
 * return URL参数字符串
 */
Feng.urlEncode = function(param,idx, key, encode) {
   param = param == null? "":param;
    var paramStr = '';
    var t = typeof (param);
    if (t == 'string' || t == 'number' || t == 'boolean') {
        var one_is =idx<3?'?':'&';
        paramStr += one_is + key + '=' + ((encode==null||encode) ? encodeURIComponent(param) : param);
    } else {
        for (var i in param) {
            var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
            idx++;
            var v = param[i];
            if(v instanceof Array){
                v = v.join(",");
            }
            paramStr += Feng.urlEncode(v,idx, k, encode);
        }
    }
    return paramStr;
};